package com.lsh.dao;

import com.lsh.domain.UserTokens;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

/**
 * 用户token-Dao类（用户token实体类）
 */
@Repository
public interface UserTokenDao {

    // 注册创建user_token表
    @Insert("insert into user_tokens (uid, token, expires, createtime, logintime, status)" +
            " values (#{uid}, #{token}, #{expires}, UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), #{status})")
    void saveUserToken(UserTokens userTokens);

    // 根据uid更新user_token表
    @Update("update user_tokens set token=#{token}, logintime=UNIX_TIMESTAMP(), status=#{status}" +
            " where uid=#{uid}")
    int updateUserToken(UserTokens userTokens);

    // 根据token，status查询user_tokens表的uid值
    @Select("select uid from user_tokens where token = #{token} and status = 1")
    String getByToken(String token);

    // 更新用户登录状态
    @Update("update user_tokens set status=0 where token=#{token}")
    int updateUserStatus(String token);
}
